package com.yangjiayu.iot.system.controller;

import cn.hutool.core.convert.Convert;
import com.yangjiayu.iot.common.result.Result;
import com.yangjiayu.iot.system.pojo.SysUser;
import com.yangjiayu.iot.system.query.SysUserQuery;
import com.yangjiayu.iot.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户管理相关的控制层
 */
@RestController
@RequestMapping(value = "/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户分页查询
     * @param pageNum
     * @param pageSize
     * @param sysUserQuery
     * @return
     */
    @RequestMapping(value = "/list")
    public Result list(Integer pageNum,
        Integer pageSize,
        SysUserQuery sysUserQuery){
        return Result.ok(sysUserService.findSysUserByQuery(
            Convert.toInt(pageNum,1),
            Convert.toInt(pageSize,10),
            sysUserQuery));
    }

    /**
     * 查询用户的详细信息
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result findOne(@PathVariable(value="id") Long id){
        return Result.ok(sysUserService.findOne(id));
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @PostMapping
    public Result save(@RequestBody SysUser sysUser){
        sysUserService.save(sysUser);
        return Result.ok();
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @PutMapping
    public Result update(@RequestBody SysUser sysUser){
        sysUserService.update(sysUser);
        return Result.ok();
    }

    /**
     * 删除用户: 逻辑删除-update 不是物理删除-delete
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        sysUserService.delete(id);
        return Result.ok();
    }

}
